package com.quwan.common.service;

import com.quwan.common.result.ApiResult;
import com.quwan.common.result.OssResultDTO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lt
 * @Date 2021/12/1 12:30 下午
 */
//@RestController
//@Api(tags = "图片上传接口")
public class OssController {
    private final static Logger logger = LoggerFactory.getLogger(OssController.class);
    @Autowired
    FileUploadService fileUploadService;

    @ApiOperation(value = "uploadfFile（文件上传接口）",notes = "文件上传接口 " +
            "<br/>请求参数：file 选择文件上传 " +
            "<br/>请求参数：type: (image图片，video视频) " +
            "<br/>返回参数： " +
            "<br/>{\n" +
            "<br/> \"code\": \"0\",\n" +
            "<br/> \"msg\": \"操作成功\",\n" +
            "<br/> \"data\": [\n" +
            "<br/>     {\n" +
            "<br/>         \"fieldName\": \"任意\",\n" +
            "<br/>         \"originalName\": \"微信图片_20180817153046.jpg\",\n" +
            "<br/>         \"newName\": \"322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"relativePath\": \"image/public/322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"absPath\": \"http://ybya.oss-cn-hangzhou.aliyuncs.com/image/public/322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"suffix\": \".jpg\"\n" +
            "<br/>     }\n" +
            "<br/> ]\n" +
            "} " +
            "<br/>" +
            "")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = ApiResult.class)
    })
    @PostMapping(value="/oss/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "文件", required = true)
    })
    @ResponseBody
    public ApiResult uploadFile(HttpServletRequest request, MultipartFile file) throws Exception {
        String type = request.getParameter("type");
        logger.info("开始调用文件上传接口：imgType="+type);
        List<OssResultDTO> ossResultDTOList = fileUploadService.uploadFileOss(request, type);
//        logger.info("result:" + ossResultDTOList);
        return  ApiResult.success(ossResultDTOList);
    }


}